/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *BaseProcessDocumentGroupController
 * 文档配置组表
 *
 * 修改纪录
 * 2021-07-28 版本：1.0 chengengwei 创建。
 * @version 版本：1.0
 * @author 作者：chengengwei
 * 创建日期：2021-07-28
 */

package com.topscomm.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topscomm.base.BaseSystemConst;
import com.topscomm.cbo.pojo.CboRoleEntity;
import com.topscomm.cbo.service.ICboRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.topscomm.base.controller.auto.BaseProcessDocumentGroupControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: chengengwei
 * @date: 2021-07-28
 * @description:文档配置组表Controller
 * @modify:
 */
@RestController
@RequestMapping("base/processDocumentGroup")
public class BaseProcessDocumentGroupController extends BaseProcessDocumentGroupControllerAuto implements IBaseProcessDocumentGroupController {

	@Autowired
	private ICboRoleService cboRoleService;
	/**
	 * @author: chengengwei
	 * @date: 2021-07-28
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_businessdirection")) {
			String value = paraMap.get("qry_businessdirection").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and businessdirection = '{0}' ", value);
		}
		if (this.getUserBean().getUser().getAdminlevel() < 9) {
			List<CboRoleEntity> roles = this.cboRoleService.queryRolesByUserId(this.getUserBean().getUser().getId());

			for(int i = 0; i < roles.size(); ++i) {
				CboRoleEntity cboRoleEntity = (CboRoleEntity)roles.get(i);
				if (i == 0) {
					sql.appendSingle(" and (role like '%{0}%' ", new Object[]{cboRoleEntity.getId()});
				} else {
					sql.appendSingle(" or role like '%{0}%' ", new Object[]{cboRoleEntity.getId()});
				}
			}

			if (roles.size() > 0) {
				sql.appendSingle(")", new Object[0]);
			}
		}
		return sql.toString();
	}

	/**
	 * @author: chengengwei
	 * @date: 2021-07-28
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @author: chengengwei
	 * @date: 2021-07-28
	 * @description: 删除组信息时同时删除关联的检查项信息
	 * @modify:
	 */
	@DeleteMapping({"/deleteGroupAndDocInfo"})
	public ResponseResult<Object> deleteGroupAndDocInfo(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			this.service.deleteGroupAndDocInfo(paraMap);
			return ResponseResult.ok("删除成功");
		} catch (Exception var3) {
			return this.handleControllerException(var3);
		}
	}

	/**
	 * @author: chengengwei
	 * @date: 2021-08-10
	 * @description:查询是否有添加文件配置组的权限
	 * @modify:
	 */
	@GetMapping(value = "/queryCanEditGroupInfo")
	public ResponseResult<Object> queryCanEditGroupInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> resultMap = new HashMap<>();
			// 若当前用户为管理员或为canEditDocGroup角色则返回true
			List<CboRoleEntity> roleList = this.getUserBean().getRoleList();
			if (roleList.stream().anyMatch(item -> BaseSystemConst.EditDocGroup.equals(item.getCode())) ||
					this.getUserBean().getUser().getAdminlevel() == 9) {
				resultMap.put("canEditGroupInfo", true);
			} else {
				resultMap.put("canEditGroupInfo", false);
			}
			return ResponseResult.ok(resultMap);
		} catch (Exception e) {
			return this.handleControllerException(e);
		}
	}
}

